package org.gruposp2p.aula.gwt.client.content;

import org.gruposp2p.aula.gwt.client.AulaApplication;
import org.gruposp2p.aula.gwt.client.AulaConstants;
import org.gruposp2p.aula.gwt.client.content.course.NewCourseDialogBox;
import org.gruposp2p.aula.gwt.client.content.student.NewStudentDialogBox;

import com.google.gwt.i18n.client.Constants;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.MenuItemSeparator;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class AulaTopPanelManager {
	
	private String STYLE_NAME = "aulaTopPanelManager";
	
	public static interface CwConstants extends Constants {
		  /**
		   * @return the Reports menu options
		   */  
		  String[] mainMenuBarReportsOptions();		  
		  /**
		   * @return the name of the reports option of the main menu bar 
		   */  
		  String mainMenuBarReportsCategory ();
		  
		  String mainMenuBarCreateObjectCategory ();
		  
		  String mainMenuBarCreateNewCourse ();
		  
		  String mainMenuBarCreateNewCoursegroup ();
		  
		  String mainMenuBarCreateNewStudent ();
		  
		  String mainMenuBarAdminCategory ();
		  
		  String authenticateRegisterLabel ();
	}
	
	private AulaApplication aulaApplication;
	private MenuBar menuBar;
	private MenuItem enterMenuItem;
	private MenuBar adminMenu;
	  /**
	   * An instance of the constants.
	   */
	  private AulaConstants constants;	
	
	public AulaTopPanelManager (AulaApplication aulaApplication) {
		this.aulaApplication = aulaApplication;
		this.constants = aulaApplication.getConstants();
		menuBar = new MenuBar();
		menuBar.setAutoOpen(true);
		menuBar.setAnimationEnabled(true);
		menuBar.setWidth("100%");
	}

	private MenuBar getAuthenticatedMenuBar () {
		menuBar.clearItems();
	    // Create the reports menu
		MenuBar reportsMenu = new MenuBar(true);
		reportsMenu.setAnimationEnabled(true);
		MenuItem reportsMenuItem = new MenuItem(
				constants.mainMenuBarReportsCategory(), reportsMenu);
		menuBar.addItem(reportsMenuItem);
		String[] reportsMenuOptions = 
			constants.mainMenuBarReportsOptions();
		for (int i = 0; i < reportsMenuOptions.length; i++) {
			reportsMenu.addItem(reportsMenuOptions[i], reportCommand);
		}
		/*
	    MenuItemSeparator separator1 = new MenuItemSeparator();
		separator1.setStyleName("aulaMainMenuBarSeparator");
		separator1.setWidth("500px");
		addSeparator(separator1);*/    	  
		// Create the reports menu
		MenuBar createObjectMenu = new MenuBar(true);
		createObjectMenu.setAnimationEnabled(true);
		menuBar.addItem(new MenuItem(
				constants.mainMenuBarCreateObjectCategory(), createObjectMenu));	    
		
		// Create the reports menu
		/*
		MenuBar adminMenu = new MenuBar(true);
		adminMenu.setAnimationEnabled(true);
		menuBar.addItem(new MenuItem(
				constants.mainMenuBarAdminCategory(), adminMenu));*/
			    
	    MenuItemSeparator separator = new MenuItemSeparator();
	    separator.setStyleName("aulaTopPanelSeparator");
		separator.setWidth("100%");
		menuBar.addSeparator(separator);
		menuBar.addItem(constants.authenticateRegisterLabel(), 
				authenticateCommand);
		return menuBar;
	  }
	
	public final Widget createWidget() {
		VerticalPanel vPanel = new VerticalPanel();
		if (aulaApplication.getAulaUser() != null && 
				aulaApplication.getAulaUser().isAuthenticated()) {
			vPanel.add(getAuthenticatedMenuBar());
		} else {
			vPanel.add(getUnAuthenticatedMenuBar());
		}
		vPanel.setWidth("100%");
		vPanel.setStyleName(STYLE_NAME);
		return vPanel;
	}

    private MenuBar getUnAuthenticatedMenuBar () {
		menuBar.clearItems();
	    // Create the reports menu
		MenuBar reportsMenu = new MenuBar(true);
		reportsMenu.setAnimationEnabled(true);
		MenuItem reportsMenuItem = new MenuItem(
				constants.mainMenuBarReportsCategory(), reportsMenu);
		menuBar.addItem(reportsMenuItem);
		String[] reportsMenuOptions = 
			constants.mainMenuBarReportsOptions();
		for (int i = 0; i < reportsMenuOptions.length; i++) {
			reportsMenu.addItem(reportsMenuOptions[i], reportCommand);
		}
		/*
	    MenuItemSeparator separator1 = new MenuItemSeparator();
		separator1.setStyleName("aulaMainMenuBarSeparator");
		separator1.setWidth("500px");
		addSeparator(separator1);*/    	  
		MenuBar createObjectMenu = new MenuBar(true);
		createObjectMenu.setAnimationEnabled(true);
		MenuItem createObjectMenuItem = new MenuItem(
				constants.mainMenuBarCreateObjectCategory(), createObjectMenu);
		menuBar.addItem(createObjectMenuItem);	
		createObjectMenu.addItem(constants.mainMenuBarCreateNewCourse(), newCourseCommand);
		createObjectMenu.addItem(constants.mainMenuBarCreateNewStudent(), newStudentCommand);
		
		// Create the reports menu
		adminMenu = new MenuBar(true);
		adminMenu.setAnimationEnabled(true);
		menuBar.addItem(new MenuItem(constants.mainMenuBarAdminCategory(), adminMenu));
		adminMenu.addItem(constants.mainMenuBarAdminCategory(), adminItemevaluablesCommand);	    
		
	    MenuItemSeparator separator = new MenuItemSeparator();
	    separator.setStyleName("aulaTopPanelSeparator");
		separator.setWidth("100%");
		//menuBar.addSeparator(separator);
		enterMenuItem = new MenuItem(constants.authenticateRegisterLabel(), 
				authenticateCommand);
		menuBar.addItem(enterMenuItem);
		return menuBar;
	}	

	Command authenticateCommand = new Command() {
        public void execute() {/*
			if (aulaUser != null && aulaUser.isAuthenticated()) {
		    } else {
		    }*/
        	AulaAuthenticateDialogBox dialogBox = 
        		new AulaAuthenticateDialogBox (aulaApplication);
        	dialogBox.showRelativeTo(enterMenuItem);
        	dialogBox.show();
		}
    };	
		    
	Command reportCommand = new Command() {
	   public void execute() {/*
				if (aulaUser != null && aulaUser.isAuthenticated()) {
			    } else {			    		  
			    }*/
			}
		};

	Command newCourseCommand = new Command() {
		public void execute() {
			NewCourseDialogBox newCourseDialogBox = new NewCourseDialogBox (aulaApplication);
			newCourseDialogBox.center();
			newCourseDialogBox.show();
		}
	};		
		
	Command newStudentCommand = new Command() {
		public void execute() {
			NewStudentDialogBox newStudentDialogBox = new NewStudentDialogBox (aulaApplication);
			newStudentDialogBox.center();
			newStudentDialogBox.show();
		}
	};	
	
	Command adminItemevaluablesCommand = new Command() {
	   public void execute() {
		   ItemevaluablesDialogBox itemevaluablesDialogBox =
			   new ItemevaluablesDialogBox(aulaApplication);
		   itemevaluablesDialogBox.center();
		   itemevaluablesDialogBox.show();
		}
	};			
		  
}
